﻿using _2302MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace _2302MVCDemo.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        //2.视图向控制器传值【推荐用法】(string name,string pwd)
        [HttpPost]//特性
        public ActionResult Index(Student model)//【推荐用法】
        {
            //如果View()没有参数，则返回和方法名同名的视图，即Index.cshtml
            //return View();
            //如果有一个参数，则返回该参数名的视图

            //1.视图向控制器传值（通过表单）
            //string name = Request.Form["name"].ToString();
            //string pwd = Request.Form["pwd"].ToString();

            if (model.Name == "admin" && model.Pwd == "123456")
            {
                //允许授权
                FormsAuthentication.SetAuthCookie(model.Name,false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                //跨越控制器跳转
                return RedirectToAction("Index", "Login");
            }
            //ViewBag.name = model.Name;
            //ViewBag.pwd = model.Pwd;
            //return View();
        }

        public ActionResult Home()
        {
            //从控制器向视图传值，不可以跨越ActionResult
            ViewData["name"] = "张三";
            ViewBag.age = 20;
            ViewBag.name = "李四";
            ViewData["age"] = 30;
            //可以跨越ActionResult，只能取值一次
            TempData["sex"] = "男";

            Student xiaoHong = new Student();
            xiaoHong.Name = "小红";
            xiaoHong.Age = 21;
            xiaoHong.Sex = "女";

            ViewBag.user = xiaoHong;

            return View();
        }
    }
}